//
//  HBCategoryController.m
//  HeartBeat
//
//  Created by karong on 2023/9/18.
//

#import "HBCategoryController.h"
#import "HBCategoryListCell.h"
#import "HBCategoryModel.h"
#import "HBChannelModel.h"
#import "HBCategoryHeadView.h"
#import "HBChannelRequest.h"
#import "HBChannelItemCell.h"

@interface HBCategoryController () <HBCategoryListCellDelegate, HBCategoryHeadViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) HBCategoryModel * categoryModel;
@property (nonatomic, strong) HBChannelRequest * request;
@end

@implementation HBCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.clearColor;
    HBChannelRequest * request = [HBChannelRequest new];
    self.request = request;
    UICollectionViewFlowLayout * flowLayout  = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.sectionHeadersPinToVisibleBounds = YES;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.listView.collectionViewLayout = flowLayout;
    [self loadCategoryRequest];
}

- (BOOL)disableAutoBeginRefresh {
    return NO;
}

- (void)loadCategoryRequest {
    [HBRequest loadCategoryWithChannel_type:self.channel.channel_type success:^(id obj) {
        self.categoryModel = obj;
        self.categoryModel.classesH = 70;
        self.categoryModel.selectCategory.channelType = self.channel.channel_type;
        self.request.category = self.categoryModel.selectCategory;
        HBCategoryItemModel * sort_typeItem = self.categoryModel.sort_type[0];
        self.request.sort_type = sort_typeItem.id;
        [self.listView reloadData];
        [self benginRefresh];
    } failed:^(NSURLResponse *response, NSError *error, id responseObject) {
        
    }];
}
- (void)refreshData {
    [HBToast loading];
    if (!self.categoryModel) {
        [self loadCategoryRequest];
    }
    else {
        [super refreshData];
    }
   
}

- (void)loadSuccessWithArray:(NSArray *)array isRefresh:(BOOL)isRefresh {
    [HBToast dismiss];
    [super loadSuccessWithArray:array isRefresh:isRefresh];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (section == 0) {
        count = 1;
    }
    else {
        count = MAX(self.dataArray.count, 1);
    }
    return count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBBaseCollectionViewCell * cell;
    if (indexPath.section == 0) {
        HBCategoryListCell * categoryListCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBCategoryListCell" forIndexPath:indexPath];
        categoryListCell.delegate = self;
        [categoryListCell setDataWithObject:self.categoryModel];
        cell = categoryListCell;
    }
    else {
        if (self.dataArray.count == 0) {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBCategoryEmptyCell" forIndexPath:indexPath];
        }
        else {
            HBChannelItemCell *newestCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBChannelItemCell" forIndexPath:indexPath];
            [newestCell setDataWithObject:self.dataArray[indexPath.row]];
            cell = newestCell;
        }
        
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    if (section == 1) {
        size.height = 45 + 12;
    }
    return size;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        HBCategoryHeadView * categoryHeadView;
        if (!categoryHeadView) {
            categoryHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HBCategoryHeadView" forIndexPath:indexPath];
            categoryHeadView.delegate = self;
            [categoryHeadView setDataWithObject:self.categoryModel];
        }
        return categoryHeadView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth;
    if (indexPath.section == 0) {
        size.height = self.categoryModel.classesH;
    }
    else {
        if (self.dataArray.count == 0) {
            size.height = 180;
        }
        else {
            size.height = 92.5 + 12;
        }
    }
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && self.dataArray.count > 0) {
        [ViewManager pushNovelDetailVCWithNovel:self.dataArray[indexPath.row] inController:self];
    }
}
#pragma mark - HBCategoryListCellDelegate
- (void)categoryListCellRefreshHeight:(NSInteger)height {
    self.categoryModel.classesH = height;
    [self.listView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
}

- (void)categoryListCellDidSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self refreshData];
}

#pragma mark - HBCategoryHeadViewDelegate
- (void)categoryHeadViewDidSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HBCategoryItemModel * sort_typeItem = self.categoryModel.sort_type[indexPath.row];
    self.request.sort_type = sort_typeItem.id;
    [self refreshData];
}

@end
